import moment from 'moment'

const dictData = {
  WMS_Owner: [
    {
      ParentID: 0,
      caption: 'H001 华东仓货主001',
      oid: 12124,
      OID: 12124,
      WarehouseID: 10034,
      Code: 'WMS_12124',
      itemKey: 'WMS_Owner',
      UseGroupUniConfig: 0,
      Name: '华东仓货主001',
      GroupUniCode: 'H001',
      UniCode: 'H001',
      NodeType: 0,
      Enable: 1
    },
    {
      ParentID: 0,
      UniCode: 'H002',
      NodeType: 0,
      caption: 'H002 华东仓货主002',
      Enable: 1,
      oid: 12163,
      OID: 12163,
      WarehouseID: 10034,
      Code: 'WMS_12163',
      itemKey: 'WMS_Owner',
      UseGroupUniConfig: 0,
      Name: '华东仓货主002'
    }
  ],
  WMS_Warehouse: [
    {
      ParentID: 0,
      CompanyID: 10031,
      NodeType: 0,
      caption: '1001 华东仓',
      Enable: 1,
      oid: 10034,
      OID: 10034,
      Code: 'WMS_10034',
      WarehouseCode: '1001',
      itemKey: 'WMS_Warehouse',
      Name: '华东仓'
    },
    {
      ParentID: 0,
      CompanyID: 10031,
      NodeType: 0,
      caption: '1002 华南仓',
      Enable: 1,
      oid: 10037,
      OID: 10037,
      Code: 'WMS_10037',
      WarehouseCode: '1002',
      itemKey: 'WMS_Warehouse',
      Name: '华南仓'
    },
    {
      ParentID: 0,
      CompanyID: 10031,
      NodeType: 0,
      caption: '1003 华中仓',
      Enable: 1,
      oid: 10038,
      OID: 10038,
      Code: 'WMS_10038',
      WarehouseCode: '1003',
      itemKey: 'WMS_Warehouse',
      Name: '华中仓'
    },
    {
      ParentID: 0,
      CompanyID: 10031,
      NodeType: 0,
      caption: '2001 华北仓',
      Enable: 1,
      oid: 10039,
      OID: 10039,
      Code: 'WMS_10039',
      WarehouseCode: '2001',
      itemKey: 'WMS_Warehouse',
      Name: '华北仓'
    },
    {
      ParentID: 0,
      CompanyID: 10031,
      NodeType: 0,
      caption: '2002 西北仓',
      Enable: 1,
      oid: 10040,
      OID: 10040,
      Code: 'WMS_10040',
      WarehouseCode: '2002',
      itemKey: 'WMS_Warehouse',
      Name: '西北仓'
    }
  ]
}
/**
 * 字典控件：
   Format: {
    controlType: 'Dict',
    itemKey: 'Operator',
    fieldKey: 'Name'
  }
 */
const GetDictValue2 = (ItemKey, OID, FieldKey) => {
  let value = OID
  if (FieldKey) {
    let dictList = dictData[ItemKey]
    value = dictList.find((f) => f.OID == OID)[FieldKey]
  }
  console.log('value', value)
  return value
}
const GetDictValue = (ItemKey, OID, FieldKey, colIndex) => {
  let value = OID
  if (ItemKey && OID && FieldKey && window.hiprint.hiprintExpData) {
    let str = ItemKey + '_' + OID + '_' + FieldKey
    value = window.hiprint.hiprintExpData.DictCaptions[str] || value
  }
  // console.log('DictValue', value)
  return value
}

const GetDictValueYigo = (ItemKey, OID, FieldKey) => {
  let value = OID
  let promise = OID
  let formID = window.hiprint.query.formID
  console.log('GetDictValue', formID, ItemKey, OID, FieldKey)
  promise = parent.exec(formID, `GetDictValue('${ItemKey}',${OID},'${FieldKey}')`)
  const a = promise.then((res) => {
    console.log('res', res)
    return res
  })
  console.log('a', a)
  console.log('promise', promise)
  console.log('value', value)
  return promise
}

const GetTextEditorValue = () => {}
const GetComboBoxValue = (ComboBoxItems, value) => {
  // console.log('ComboBoxItems', ComboBoxItems, value)
  if (ComboBoxItems.length > 0) {
    value = ComboBoxItems.find((f) => f.Value == value).Caption || value
  }
  return value
}
const GetDatePickerValue = (dateFormat, value) => {
  value = moment(new Date(value)).format(dateFormat) || value
  // console.log('DatePickerValue：', value)
  return value
}
const GetNumberEditorValue = () => {}

const ControlType = {
  TextEditor: GetTextEditorValue,
  Dict: GetDictValue,
  ComboBox: GetComboBoxValue,
  DatePicker: GetDatePickerValue,
  NumberEditor: GetNumberEditorValue
}
const GetAttributeValue = (options, value, colIndex) => {
  // let formID = window.hiprint.query.formID
  // console.log('formID', formID)
  let config = {}
  if (options.attributeConfig) {
    config = JSON.parse(options.attributeConfig)
  } else if (parseInt(colIndex) >= 0 && options.columns[0].columns[colIndex].attributeConfig) {
    config = JSON.parse(options.columns[0].columns[colIndex].attributeConfig)
  }
  if (config.controlType && value) {
    let type = config.controlType
    let res = value
    if (type == 'Dict' && config.itemKey) {
      res = ControlType[type](config.itemKey, value, config.fieldKey, colIndex)
    }
    if (type == 'ComboBox' && config.ComboBoxItems) {
      res = ControlType[type](config.ComboBoxItems, value)
    }
    if (config.dateFormat && type == 'DatePicker') {
      res = ControlType[type](config.dateFormat, value)
    }
    return res
  } else {
    return value
  }
}

const printFuns = {
  GetAttributeValue: GetAttributeValue
}
export default printFuns
